﻿using System.Text;
using System.Web;
using System.Web.Mvc;

namespace AutoClub.Support
{
    public static class HtmlExtensions
    {
        public static HtmlString ImageMenu(this HtmlHelper html, string action, object routeValues, string imagePath, string alt, string linkDescription, string description)
        {
            var url = new UrlHelper(html.ViewContext.RequestContext);
            string computedAction = string.IsNullOrWhiteSpace(action) ? "#" : action;
            // build the <img> tag
            var imgBuilder = new TagBuilder("img");
            imgBuilder.MergeAttribute("src", url.Content(imagePath));
            imgBuilder.MergeAttribute("alt", alt);
            string imgHtml = imgBuilder.ToString(TagRenderMode.SelfClosing);

            // build the <span active> tag
            var spanActiveBuilder = new SpanBuilder("active");

            // build the <span wrap> tag
            var spanWrapBuilder = new SpanBuilder("wrap");

            // build the <span link> tag
            spanWrapBuilder.AddNestedSpan("link", linkDescription);
            spanWrapBuilder.AddNestedSpan("descr", description);

            StringBuilder outerSpanBuilder = new StringBuilder(imgHtml);
            outerSpanBuilder.Append(spanActiveBuilder.ToString());
            outerSpanBuilder.Append(spanWrapBuilder.ToString());

            // build the <a> tag
            var anchorBuilder = new TagBuilder("a");
            anchorBuilder.MergeAttribute("href", computedAction);
            anchorBuilder.InnerHtml = outerSpanBuilder.ToString(); // include the <img> tag inside
            string anchorHtml = anchorBuilder.ToString(TagRenderMode.Normal);

            return MvcHtmlString.Create(anchorHtml);
        }
    }
}